package com.cosmic.pizza.web.servlet;

import com.cosmic.pizza.dao.OrderDao;
import com.cosmic.pizza.pojo.*;
import com.cosmic.pizza.service.Impl.OrderServiceImpl;
import com.cosmic.pizza.service.OrderService;
import com.cosmic.pizza.util.SessionGetListUtils;
import com.cosmic.pizza.util.UuidUtil;
import com.cosmic.pizza.web.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/Order/*")
public class OrderServlet extends BaseServlet {
    OrderService orderService = new OrderServiceImpl();
    public void FindOrdersByCurrentPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute("user");
        int uid = user.getUid();
        String currentPage = req.getParameter("currentPage");
        if (currentPage == null || currentPage.equals("")) {
            currentPage = "1";
        }
        PageBean<Order> allOrders = orderService.findAllOrder(uid, Integer.parseInt(currentPage));
        writeValue(resp,allOrders);
    }
    public void cartOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ResultInfo info = new ResultInfo();
        //Order类
        Order order = new Order();
        //setOrder类
        String oid = UuidUtil.getUuid();


        Integer add_id = Integer.parseInt(req.getParameter("add_id"));

        order.setOid(oid);
        User user = (User) req.getSession().getAttribute("user");
        int uid = user.getUid();
        order.setUid(uid);
        order.setDelivery("美团小哥");
        Double price = (double) 0;
        /*order.setTotal_price(Double.valueOf(req.getParameter("total_price")));*/
        order.setGetFlag("N");
        order.setPayFlag("Y");
        //setOrderInfo类
        List<OrderInfo> cart = SessionGetListUtils.castList(req.getSession().getAttribute("cart"),OrderInfo.class);
        //OrderInfo类
        if (cart!=null){
            for (OrderInfo orderInfo : cart){
                price += orderInfo.getFood().getPrice()*orderInfo.getSum();
                orderInfo.setOid(oid);
            }
            order.setTotal_price(price);
            //调用Service层方法
            orderService.CartOrder(order,cart);
            req.getSession().setAttribute("OrderId",oid);
            orderService.checkOutOrder(oid,add_id);
            info.setFlag(true);
            req.getSession().removeAttribute("cart");
        }else {
            info.setFlag(false);
        }
        writeValue(resp,info);
        /*List<OrderInfo> orderInfos = new ArrayList<OrderInfo>();
        for (int i = 0 ; i < count ; i++){
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setOid(oid);
            orderInfo.setFid(cart.get(i).getFid());
            orderInfo.setSum(cart.get(i).getSum());
            orderInfo.setSize(cart.get(i).getSize());
            orderInfos.add(i,cart.get(i));
        }*/
    }
    public void CheckOutOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String oid = (String) req.getSession().getAttribute("OrderId");
        Integer add_id = Integer.valueOf(req.getParameter("add_id"));
        orderService.checkOutOrder(oid,add_id);
    }
    public void PayOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ResultInfo info = new ResultInfo();
        String oid = (String) req.getParameter("oid");
        info.setFlag(orderService.PayOrder(oid));
        writeValue(resp,info);
    }
    public void GetFlagOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ResultInfo info = new ResultInfo();
        String oid = (String) req.getParameter("oid");
        info.setFlag(orderService.GetFlagOrder(oid));
        writeValue(resp,info);
    }
    public void GetNoneGetOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        /*ResultInfo info = new ResultInfo();*/
        User user = (User) req.getSession().getAttribute("user");
        int uid = user.getUid();
        List<Order> noGetOrders = orderService.findNoneGetOrder(uid);
        /*info.setFlag(noGetOrders.size() > 0);
        info.setData(noGetOrders);*/
        writeValue(resp,noGetOrders);
    }
    public void GetNonePayOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ResultInfo info = new ResultInfo();
        User user = (User) req.getSession().getAttribute("user");
        int uid = user.getUid();
        List<Order> noGetOrders = orderService.findNonePayOrder(uid);
        info.setFlag(noGetOrders.size() > 0);
        info.setData(noGetOrders);
        writeValue(resp,info);
    }
    public void GetOrderByOid(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String oid = (String) req.getParameter("oid");
        Order order = orderService.findOrderByOid(oid);
        writeValue(resp,order);
    }
    public void evalOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String evalMes = req.getParameter("evalMes");
        String oid = req.getParameter("oid");
        String fid = req.getParameter("fid");
        String size = req.getParameter("size");
        orderService.eval(oid,fid,size,evalMes);
        writeValue(resp,true);
    }
    public void ifEval(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String oid = req.getParameter("oid");
        String fid = req.getParameter("fid");
        String size = req.getParameter("size");
        boolean flag = orderService.ifEval(oid,fid,size);
        writeValue(resp,flag);
    }
}
